package com.luck.service;

import com.luck.api.BlogManager;
import com.luck.dao.BlogMapper;
import com.luck.model.po.Blog;
import com.luck.model.pojo.PageRequest;
import com.luck.model.pojo.PageResponse;
import com.luck.model.dto.BlogInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hua wb on 2018/5/16.
 */
@Service
public class BlogService implements BlogManager{
    @Autowired
    private BlogMapper blogMapper;
    @Override
    public Integer addBlog(Blog blog) {
        return blogMapper.insertSelective(blog);
    }

    @Override
    public BlogInfoDto getBlog(Integer blogId) {
        return blogMapper.getBlogInfo(blogId);
    }

    @Override
    public List<Blog> getBlogs() {
        return blogMapper.getBlogs();
    }

    @Override
    public PageResponse<BlogInfoDto> getPageBlogInfo(PageRequest pageRequest) {
        Integer count = blogMapper.getPageBlogInfoCount();
        if (count == null || count == 0) {
            return new PageResponse<>(pageRequest.getIndex(), 0, new ArrayList<>());
        }
        List<BlogInfoDto> pageBlogInfo = blogMapper.getPageBlogInfo(pageRequest);
        return new PageResponse<>(pageRequest.getIndex(), count, pageRequest.getLimit(), pageBlogInfo);
    }
}
